package com.aglow.bluetoothspeaker.main.ui.activity;



import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.main.base.BaseActivity;
import com.aglow.bluetoothspeaker.model.cache.MusicCache;
import com.aglow.bluetoothspeaker.presenter.service.PlayService;
import com.aglow.bluetoothspeaker.utils.BluzDeviceUtils;
import com.aglow.bluetoothspeaker.utils.BluzManagerUtils;
import com.aglow.bluetoothspeaker.utils.SpManager;

import java.util.Calendar;

/**
*
*
* Author: chufeng
* Email: 392044178@qq.com
* Date: 2018/9/18 15:16
 *Depict:启动页面
*
*/
public class SplashActivity extends BaseActivity {
    private final static int REQUEST_BLUETOOTH_ON = 100;
    private ServiceConnection mPlayServiceConnection;
    /**
     * 蓝牙打开操作执行完成
     */
    private boolean isBluetoothOpenFinish;


    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void onBackClick() {

    }

    @Override
    public void initData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        },500);

    }

    @Override
    public void initView() {
        SpManager.getInstance().saveMusicListUpdateFinish(false);
        Intent serviceIntent = new Intent(mContext, PlayService.class);
        startService(serviceIntent);
        mPlayServiceConnection = new PlayServiceConnection();
        bindService(serviceIntent, mPlayServiceConnection, Context.BIND_AUTO_CREATE);
        Log.i("sp","ing....");

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_BLUETOOTH_ON) {
            isBluetoothOpenFinish = true;
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void addViewListener() {

    }


    private class PlayServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            final PlayService playService = ((PlayService.PlayBinder) service).getService();
            Log.i("PlayServiceConnection","playService = "+playService);
            MusicCache.setPlayService(playService);
            playService.updateMusicList(null);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mPlayServiceConnection);

    }
}
